import { Controller, UseGuards } from '@nestjs/common';
import { Crud, CrudAuth, Override, ParsedRequest, ParsedBody, CrudController, CrudRequest } from "@nestjsx/crud";
import { ApiTags, ApiBearerAuth, ApiBody} from '@nestjs/swagger';
import { UserCoupon } from "../../entities/user_coupon.entities";
import { UserCouponService } from '../../services/user-coupon/user-coupon.service';
import { AuthGuard } from '@nestjs/passport';

@Crud({
    model:{
      type: UserCoupon
    },
    query:{
      join:{
        coupon:{
          exclude:["send_type"],
          eager:true
        },
        user:{
          exclude:[]
        },
        order:{
          exclude:[]
        }
      },
      exclude:["coupon_id","user_id","order_id"]
    }
})
@CrudAuth({
  property:"user",
  filter: (user) => ({
    user_id: user.userId
  }),
  persist:(user) => {
    return user;
  }
})
@UseGuards(AuthGuard('jwt'))
@ApiBearerAuth()
@ApiTags('user-coupon')
@Controller('api/user-coupon')
export class UserCouponController {
  constructor(
    public service:UserCouponService
  ){}
  
  get base(): CrudController<UserCoupon> {
    return this;
  }

  @ApiBody({
    type: UserCoupon
  })
  @Override()
  createOne(
    @ParsedRequest() req: CrudRequest,
    @ParsedBody() dto: any): Promise<UserCoupon> {
    dto.user_id=req.parsed.authPersist.userId
    return this.base.createOneBase(req, dto);
  }

  @ApiBody({
    type: UserCoupon
  })
  @Override()
  updateOne(
    @ParsedRequest() req: CrudRequest,
    @ParsedBody() dto: any): Promise<UserCoupon> {
    dto.user_id=req.parsed.authPersist.userId
    return this.base.updateOneBase(req, dto);
  }
}
